package com.nbicc.attachments.service.impl;

import com.nbicc.attachments.mapper.AttachmentMapper;
import com.nbicc.attachments.po.Attachment;
import com.nbicc.attachments.service.IAttachmentService;
import com.nbicc.common.sequence.Sequence;
import com.nbicc.file.bo.FileDesc;
import com.nbicc.file.service.IFileService;
import com.nbicc.newsattachments.po.NewsAttachments;
import com.nbicc.newsattachments.service.INewsAttachmentsService;
import com.nbicc.newsattachments.service.impl.NewsAttachmentsServieImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lilingyan
 */
@Service("AttachmentServiceImpl")
public class AttachmentServiceImpl implements IAttachmentService{

    @Autowired
    private Sequence sequence;
    @Autowired
    private AttachmentMapper attachmentMapper;
    @Resource(name = "FileServiceImpl")
    private IFileService fileService;
    @Resource(name = "NewsAttachmentsServieImpl")
    private INewsAttachmentsService newsAttachmentsService;

    @Transactional
    @Override
    public Attachment uploadFile(HttpServletRequest request, MultipartFile uploadFile) throws Exception{
        FileDesc fileDesc = fileService.queryUploadFileDesc(request , uploadFile);
        fileService.uploadFile(fileDesc , uploadFile.getInputStream());

        Attachment attachment = new Attachment();
        attachment.setId(sequence.nextId());
        attachment.setName(uploadFile.getOriginalFilename());
        attachment.setSize(uploadFile.getSize());
        attachment.setFileId(fileDesc.getId());
        attachment.setCreateTime(new Date());
        attachmentMapper.insert(attachment);
        return attachment;
    }

    @Override
    public void downloadFile(HttpServletRequest request, HttpServletResponse response, long id) throws Exception{
        Attachment attachment = attachmentMapper.selectById(id);
        if(attachment == null){
            throw new IOException("没有此附件");
        }

        FileDesc fileDesc = fileService.queryDownloadFileDesc(attachment.getFileId(), request);
        response.setHeader("Content-Disposition", "attachment;filename="+new String(attachment.getName().getBytes("utf-8"),"iso8859-1"));
        fileService.downloadFile(fileDesc , response.getOutputStream());
    }

    @Transactional
    @Override
    public void deleteFile(long id) throws IOException {
        Attachment attachment = attachmentMapper.selectById(id);
        attachmentMapper.deleteById(id);
        fileService.deleteFile(attachment.getFileId());     //删除文件系统中的文件
        Map<String , Object> parms = new HashMap<>();
        parms.put(NewsAttachments.FIELD_ATTACHMENT_ID, attachment.getId());
        newsAttachmentsService.deleteByMap(parms);          //删除与新闻的关系
    }
}
